package com.scalar.chiptrack.finance.openpo.actioncommands;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.users.dao.UserDAO;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.commons.ClientInfo;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderReceiptForm;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.ScalarException;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * Created KumaraSwamy.
 * Date: Feb 28, 2005
 * Time: 9:13:42 PM
 */
public class PurchaseOrderReceiptPrinterFriendlyCommand extends ChipTrackExtendedAction
{

    public ActionForward executeRequest( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        HashMap errors			= ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info            = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String actionForward 	= "printerFriendly";
		String action   		= request.getParameter( REQUEST_ACTION );
        String receiptId        = request.getParameter ( "receiptId");

		HttpSession mSession 	= request.getSession();
		PurchaseOrderReceiptForm purchaseOrderReceiptForm = ( PurchaseOrderReceiptForm ) form;

		try
		{
			if( action.equalsIgnoreCase( "printerFriendly" ) )
			{
                try
                {
                    purchaseOrderReceiptForm.setReceipt_id( Integer.parseInt( receiptId) );
                }
                catch( Exception e)
                {
                    purchaseOrderReceiptForm.setReceipt_id(0);
                }
                try{
                    PurchaseOrderDAO.loadPurchaseOrderReceipt( purchaseOrderReceiptForm ) ;
                    ClientInfo clientInfo = UserDAO.getClientAddressInfo();
                    purchaseOrderReceiptForm.setClientInfo( clientInfo);
                }
                catch( ScalarException se)
                {
                    Logger.error( ERROR_LOADING_RECEIVINGS_TRANSACTION_INFORMATION, se );
                    errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_LOADING_RECEIVINGS_TRANSACTION_INFORMATION );
                    se.printStackTrace();
                }
				actionForward = "printerFriendly";
			}
		}
		catch ( Exception e )
		{
			Logger.error( ERROR_LOADING_RECEIVINGS_TRANSACTION_INFORMATION, e );
			errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_LOADING_RECEIVINGS_TRANSACTION_INFORMATION );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
			actionForward = "receipt";
		}
		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
    }

}
